﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Windows.Data;
using System.Globalization;
using System.Windows;

namespace SettlersEngine
{

    [ValueConversion(typeof(DateTime), typeof(String))]
    internal class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is DateTime))
                return "(unknown)";

            DateTime time = ((DateTime)value);

            if (time == DateTime.MinValue)
                return "";

            return time.ToString("y", System.Threading.Thread.CurrentThread.CurrentCulture);

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }

    [ValueConversion(typeof(Duration), typeof(String))]
    internal class DurationConverter : IValueConverter
    {
        internal static String TwoChar(Int32 value)
        {
            if (value > 9)
                return value.ToString();
            else
                return "0" + value;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is TimeSpan))
                return "(unknown)";

            TimeSpan s = (TimeSpan)value;

            String result = "";
            Boolean hasValue = false;

            if (s.Days > 0)
            {
                result += TwoChar(s.Days) + ".";
                hasValue = true;
            }

            if ((s.Hours > 0) || hasValue)
            {
                result += TwoChar(s.Hours) + ":"; 
                hasValue = true;
            }

            if ((s.Minutes > 0) || hasValue)
            {
                result += TwoChar(s.Minutes) + ":"; 
                hasValue = true;
            }

            result += TwoChar(s.Seconds);

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }

    [ValueConversion(typeof(Double), typeof(Int32))]
    internal class DoubleRoundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Double))
                return 0;

            return (Int32)((Double)value);

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Int32)
                return (Double)(Int32)value;
            else if (value is Int64)
                return (Double)(Int64)value;
            else if (value is String)
                return Double.Parse((String)value);
            else
                return (Double)value;
        }
    }

    [ValueConversion(typeof(DateTime), typeof(String))]
    internal class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is DateTime))
                return "(unknown)";

            DateTime time = ((DateTime)value);

            if (time == DateTime.MinValue)
                return "";

            return time.ToString("g", System.Threading.Thread.CurrentThread.CurrentCulture);

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }

    [ValueConversion(typeof(Boolean), typeof(String))]
    internal class BooleanReadonlyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Boolean))
                return "(unknown)";

            if ((Boolean)value)
                return "Yes";
            else
                return "No";

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }


    [ValueConversion(typeof(Int64), typeof(String))]
    internal class MediaIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(!(value is Int64))
                return "(unknown)";

            Int64 mediaID = (Int64)value;

            return mediaID.ToString("X");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String mediaID = value as String;

            if (mediaID == null)
                return 0;

            return Int64.Parse(mediaID, NumberStyles.HexNumber);
        }
    }

    [ValueConversion(typeof(IEnumerable<String>), typeof(String))]
    internal class StringListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            IEnumerable<String> list = value as IEnumerable<String>;
            String result = "";

            if (list == null)
                return "";

            foreach (String s in list)
            {
                result += s + ", ";
            }

            if (result.EndsWith(", "))
                result = result.Substring(0, result.Length - 2);

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String source = value as String;

            if (source == null)
                return new List<String>();

            return (from e in source.Split(',') orderby e ascending select e.Trim()).ToArray();
        }
    }
}
